package com.augurit.gzcgwsp.zzgqba.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.hwgz.convert.YwHwShSqbConvertor;
import com.augurit.gzcgwsp.hwgz.entity.YwHwShSqb;
import com.augurit.gzcgwsp.qyzz.entity.YwRqQyzzInfo;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjclForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.gzcgwsp.zzgqba.convert.YwRqZzgqbaConvertor;
import com.augurit.gzcgwsp.zzgqba.dao.YwRqZzgqbaDao;
import com.augurit.gzcgwsp.zzgqba.entity.YwRqZzgqba;
import com.augurit.gzcgwsp.zzgqba.service.IYwRqZzgqbaService;
import com.augurit.gzcgwsp.zzgqba.web.form.YwRqZzgqbaForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;

@Service
@Transactional
public class YwRqZzgqbaServiceImpl  extends WfBusClientServiceImpl<YwRqZzgqbaForm> implements IYwRqZzgqbaService{

	@Autowired
	private YwRqZzgqbaDao ywRqZzgqbaDao;
	
	@Autowired
    private IOaDocCodeService doccodeService;
	@Autowired
	private IYwComSjcllistService ywComSjcllistService;
	@Autowired
	private IYwComSjclService ywComSjclService;
	@Autowired
    private IYwMtbjlshInfoService ywMtbjlshInfoService;
	@Autowired
    private IYwComSjclService sjclService;
	
	
	/**
	 * 根据主键获取Form对象
	 */
	@Transactional(readOnly = true)
	public YwRqZzgqbaForm get(Long id) {
		YwRqZzgqba entity = ywRqZzgqbaDao.get(id);
		YwRqZzgqbaForm form=YwRqZzgqbaConvertor.convertVoToForm(entity);
		//收件材料列表
		YwComSjcllistForm sjclListForm=new YwComSjcllistForm();
		sjclListForm.setAjid(id);
		sjclListForm.setTplcode("zzgqba");
		List<YwComSjcllistForm> list=ywComSjcllistService.getList(sjclListForm);
		
		YwComSjclForm fkForm = new YwComSjclForm();
        fkForm.setAjid(id);
        fkForm.setTplcode("zzgqba");
        List<YwComSjclForm> sjclTpl = ywComSjclService.getList(fkForm);
        form.setAcceptNo2(form.getAcceptNo());
        form.setCbyw2(form.getCbyw());
        form.setLxr2(form.getLxr());
        form.setSqrq2(form.getSqrq());
        form.setSjr2(form.getSjr());
        form.setSqdw2(form.getSqdw());
        if(list.size()>0)
		{
				YwComSjcllistForm sjclForm01=list.get(0);
				form.setTitleRow101(sjclForm01.getTitle());
				form.setTitleRow102(sjclForm01.getTitle());
				form.setFyjsRow101(sjclForm01.getFyjs());
				form.setFyjsRow102(sjclForm01.getFyjs());
				form.setFsRow101(sjclForm01.getYjs()+sjclForm01.getFyjs());
				form.setFsRow102(sjclForm01.getYjs()+sjclForm01.getFyjs());
				
				YwComSjcllistForm sjclForm02=list.get(1);
				form.setTitleRow201(sjclForm02.getTitle());
				form.setTitleRow202(sjclForm02.getTitle());
				form.setFyjsRow201(sjclForm02.getFyjs());
				form.setFyjsRow202(sjclForm02.getFyjs());
				form.setFsRow201(sjclForm02.getYjs()+sjclForm02.getFyjs());
				form.setFsRow202(sjclForm02.getYjs()+sjclForm02.getFyjs());
				
				YwComSjcllistForm sjclForm03=list.get(2);
				form.setTitleRow301(sjclForm03.getTitle());
				form.setTitleRow302(sjclForm03.getTitle());
				form.setFyjsRow301(sjclForm03.getFyjs());
				form.setFyjsRow302(sjclForm03.getFyjs());
				form.setFsRow301(sjclForm03.getYjs()+sjclForm03.getFyjs());
				form.setFsRow302(sjclForm03.getYjs()+sjclForm03.getFyjs());
				
				
				YwComSjcllistForm sjclForm04=list.get(3);
				form.setTitleRow401(sjclForm04.getTitle());
				form.setTitleRow402(sjclForm04.getTitle());
				form.setFyjsRow401(sjclForm04.getFyjs());
				form.setFyjsRow402(sjclForm04.getFyjs());
				form.setFsRow401(sjclForm04.getYjs()+sjclForm04.getFyjs());
				form.setFsRow402(sjclForm04.getYjs()+sjclForm04.getFyjs());
				
				
				
				YwComSjcllistForm sjclForm05=list.get(4);
				form.setTitleRow501(sjclForm05.getTitle());
				form.setTitleRow502(sjclForm05.getTitle());
				form.setFyjsRow501(sjclForm05.getFyjs());
				form.setFyjsRow502(sjclForm05.getFyjs());
				form.setFsRow501(sjclForm05.getYjs()+sjclForm05.getFyjs());
				form.setFsRow502(sjclForm05.getYjs()+sjclForm05.getFyjs());
				
				
				
				YwComSjcllistForm sjclForm06=list.get(5);
				form.setTitleRow601(sjclForm06.getTitle());
				form.setTitleRow602(sjclForm06.getTitle());
				form.setFyjsRow601(sjclForm06.getFyjs());
				form.setFyjsRow602(sjclForm06.getFyjs());
				form.setFsRow601(sjclForm06.getYjs()+sjclForm06.getFyjs());
				form.setFsRow602(sjclForm06.getYjs()+sjclForm06.getFyjs());
		}
        
		return form;
		
	}
	
	/**
	 * 删除Form对象列表
	 */
	public void delete(Long... ids) {
		ywRqZzgqbaDao.delete(ids);
	}

	/**
	 * 保存新增或修改的Form对象列表
	 */
	public void save(YwRqZzgqbaForm... forms) {
		if(forms != null)
			for(YwRqZzgqbaForm form : forms)
				this.save(form);
	}
	
	/**
	 * 保存新增或修改的Form对象
	 */
	public void save(YwRqZzgqbaForm form){
		if(form != null){
			YwRqZzgqba entity = null;
			OaDocCodeForm doccodeForm = doccodeService.get(10L);
			if(form != null && form.getId() != null){
				entity = ywRqZzgqbaDao.get(form.getId());
				if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
			}else{
				entity = new YwRqZzgqba();
                form.setAcceptNo(null);
			}
			boolean isNewForm = false;
            if (StringUtils.isBlank(form.getAcceptNo())) {
            	//生成政务中心统一查询编号 开始
    			Map<String,String> serialNoAndPasswordMap = null;
            	if("Y".equals(form.getSendNotice())){//网办，1
            		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("1");
            	}else{//大厅，0
            		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("0");
            	}
            	String serialNo = serialNoAndPasswordMap.get("serialNo");
            	String password = serialNoAndPasswordMap.get("password");
    			form.setSerialNo(serialNo);
    			form.setPassword(password);
    			form.setAcceptNo(serialNo);
        		//生成政务中心统一查询编号 结束
                isNewForm = true;
            }
			
            YwRqZzgqbaConvertor.convertFormToVo(form, entity);			
            ywRqZzgqbaDao.save(entity);
			form.setId(entity.getId());
			if (isNewForm) {
			    sjclService.updateByNewForm(form.getId());
			}
		}
	}

	/**
	 * 根据Form对象的查询条件作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwRqZzgqbaForm> search(Page<YwRqZzgqbaForm> page, YwRqZzgqbaForm form) {
		// 建立新的分页对象
		Page pg = PageUtils.newPageInstance(page);
		
		// 查询语句及参数
		StringBuffer hql = new StringBuffer("from YwRqZzgqba ps where 1=1");
		List values = new ArrayList();
		
		// 查询条件
		if(form != null){
			
		}
		
		//排序
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// 执行分页查询操作
		ywRqZzgqbaDao.findPage(pg, hql.toString(), values);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwRqZzgqbaForm> list = YwRqZzgqbaConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * 根据过滤条件列表作分页查询
	 */
	@Transactional(readOnly = true)
	public Page<YwRqZzgqbaForm> search(Page<YwRqZzgqbaForm> page, List<PropertyFilter> filters) {
		// 建立新的分页对象
		Page<YwRqZzgqba> pg = PageUtils.newPageInstance(page);
		
		// 按过滤条件分页查找对象
		ywRqZzgqbaDao.findPage(pg, filters);
		
		// 转换为Form对象列表并赋值到原分页对象中
		List<YwRqZzgqbaForm> list = YwRqZzgqbaConvertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
    public void changeYslState(Long id,Long state) {
    	YwRqZzgqbaForm form = this.get(id);
    	form.setYsl(state);
    	this.save(form);
    }

	public void changeIsycbl(Long id, String state) {
		YwRqZzgqbaForm form = this.get(id);
    	form.setIsycbl(state);
    	this.save(form);
		
	}

	public List<YwRqZzgqba> getByAcceptNo(String acceptNo) {
    	String sql = "from YwRqZzgqba t where t.acceptNo='" + acceptNo + "'";
        List<YwRqZzgqba> entityList = ywRqZzgqbaDao.find(sql);
        return entityList;
	}
	
	/**
	 * 根据ID获得对象实体
	 * @param id
	 * @return
	 */
    
    public YwRqZzgqba getEntity(Long id){
    	return ywRqZzgqbaDao.get(id);
    }
	
	/**
     * 保存对象实体
     * @param ywRqZzgqba
     */     
    public void saveEntity(YwRqZzgqba ywRqZzgqba){
    	ywRqZzgqbaDao.save(ywRqZzgqba);
    }
}
